﻿@{
    ViewBag.Title = "库商品";
    ViewBag.PathToBtn = "/CommodityLib/List";
    ViewBag.TextBtn = "刷新";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <a id="btnPutaway" href="javascript:;" class="btn btn-success">批量入库</a>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                库商品列表
            </div>
            <div class="panel-body">
                <div class="dataTable_wrapper">
                    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th><input type="checkbox" name="select_all" value="1" id="select-all"></th>
                                <th>名称</th>
                                <th>建议价格</th>
                                <th>品牌</th>
                                <th>类别</th>
                                <th>单位</th>
                                <th>规格</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">商品入库</h4>
            </div>
            <div class="modal-body">
                <form id="form0">
                    <table class="detail-tb" style=" margin: 0 auto; border-spacing: 10px">
                        <tr>
                            <td colspan="2">
                                <div class="input-group">
                                    <input type="hidden" id="HidId" />
                                    <span class="input-group-addon">名称：</span>
                                    <input id="CommodityNameText" name="CommodityNameText" type="text" disabled="disabled" class="form-control" placeholder="请填写库存" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <div class="input-group">
                                    <input type="hidden" id="HidId1" />
                                    <span class="input-group-addon">库存：</span>
                                    <input id="Quantity" name="Quantity" type="text" class="form-control" placeholder="请填写库存" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <div class="input-group">
                                    <input type="hidden" id="HidId2" />
                                    <span class="input-group-addon">卖价：</span>
                                    <input id="SoldPrice" name="SoldPrice" type="text" class="form-control" placeholder="请填写卖价" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <div class="input-group">
                                    <input type="hidden" id="HidId3" />
                                    <span class="input-group-addon">排序：</span>
                                    <input id="DisplaySort" name="DisplaySort" type="text" class="form-control" placeholder="倒序排列，值小于100000" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style=" border:0 none">
                                <div class="input-group">
                                    <input type="hidden" id="hidParentIndex" />
                                    <input type="hidden" id="hidSubIndex" />
                                    <input type="hidden" id="CeilingPrice" />
                                    <input type="hidden" id="LowerPrice" />
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="hidOrderId" />
                <button type="button" class="btn btn-primary" id="btnSave">确认入库</button>
            </div>
        </div>
    </div>
</div>
@section javascript{
    <script src="/Content/admin/dist/js/pagingHelper.js"> </script>
    <script type="text/javascript">
        var dataParams = {};
        var serverProcessPath = "/CommodityLib/Listing";
        var showColumns = [
            { data: "Id" },
            { data: "CommodityName" },
            { data: "SuggestedPrice" },
            { data: "BrandName" },
            { data: "CategoryName" },
            { data: "Unit" },
            { data: "Spec" },
            { data: "Action" }
        ];
        var redefineColumns = [
            {
                targets: 0,
                searchable: false,
                orderable: false,
                render: function (data, type, full, meta) {
                    return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
                }
            }
        ];
        pagingHelper.Paging(serverProcessPath, dataParams, showColumns, redefineColumns);

        var table = pagingHelper.tableInstance();
        var selectedItemIds = '';

        $('#select-all').on('click', function () {
            var rows = table.rows({ 'search': 'applied' }).nodes();
            $('input[type="checkbox"]', rows).prop('checked', this.checked);
        });

        $('#dataTables-example tbody').on('change', 'input[type="checkbox"]', function () {
            if (!this.checked) {
                var el = $('#select-all').get(0);
                if (el && el.checked && ('indeterminate' in el)) {
                    el.indeterminate = true;
                }
            }
        });

        $('#btnPutaway').click(function () {
            selectedItemIds = '';
            table.$('input[type="checkbox"]').each(function () {
                if ($.contains(document, this)) {
                    if (this.checked) {
                        selectedItemIds += this.value + ",";
                    }
                }
            });
            if (selectedItemIds == "") {
                alertify.alert("请至少选择一种商品");
                return;
            }
            $.ajax({
                type: 'post',
                async: false,
                url: "/CommodityLib/AddIntoShopCommodityLib",
                data: { itemIds: selectedItemIds },
                success: function (result) {
                    if (result == true) {
                        alertify.success("添加成功，请到店铺商品列表中修改库存");
                    } else {
                        alertify.error("添加失败");
                    }
                }
            });
        });

        function CommodityStorage(id, commodityName, ceilingPrice, lowerPrice, suggestedPrice, isShopCanModifyPrice) {
            if (isShopCanModifyPrice == "false") {
                $("#SoldPrice").val(suggestedPrice).attr("disabled", "disabled");
            } else {
                $("#SoldPrice").val("");
                $("#SoldPrice").removeAttr("disabled");
            }
            $("#CeilingPrice").val(ceilingPrice);
            $("#LowerPrice").val(lowerPrice);
            $("#HidId").val(id);
            $("#CommodityNameText").val(commodityName);
        }

        $(function () {
            $("#form0").validate({
                rules: {
                    Quantity: { required: true, number: true },
                    SoldPrice: { required: true, number: true },
                    DisplaySort: { required: true, number: true, max: 100000 }
                },
                messages: {
                    Quantity: { required: "请填写库存" },
                    SoldPrice: { required: "请填写卖价" },
                    DisplaySort: { required: "请填写排序序号, 小于100000" }
                }
            });

            $("#btnSave").click(function () {

                if ($("#form0").valid()) {
                    var soldPrice = parseFloat($("#SoldPrice").val());
                    var ceilingPrice = parseFloat($("#CeilingPrice").val());
                    var lowerPrice = parseFloat($("#LowerPrice").val());
                    var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/ 
                 
                    if (lowerPrice > 0) {
                        if (soldPrice < lowerPrice) {
                            return alertify.alert('零售价格不能小于下线价格:' + lowerPrice);
                        }
                    }
                    if (ceilingPrice > 0) {
                        if (soldPrice > ceilingPrice) {
                            return alertify.alert('零售价格不能大于上限价格:' + ceilingPrice);
                        }
                    }
                
                    if (!(/(^[1-9]\d*$)/.test($("#Quantity").val()))){
                        return alertify.alert('库存请填写整数');
                    }
                if (!(/(^[1-9]\d*$)/.test($("#DisplaySort").val()))){
                        return alertify.alert('排序请填写整数');
                    }
                    $.ajax({
                        type: 'post',
                        async: false,
                        url: '/CommodityLib/CommodityStorage',
                        data: { id: $("#HidId").val(), quantity: $("#Quantity").val(), soldPrice: $("#SoldPrice").val(), displaySort: $("#DisplaySort").val(), CeilingPrice: $("#CeilingPrice").val(), LowerPrice: $("#LowerPrice").val() },
                        success: function (result) {
                            if (result.Status) {
                                alertify.success("入库成功");
                                $("#HidId").val("");
                                quantity: $("#Quantity").val("");
                                $("#SoldPrice").val("");
                                $("#CommodityNameText").val("");
                                $('#myModal').modal('hide');
                                $("#DisplaySort").val("");
                            } else {
                                alertify.error(result.Message);
                            }
                        }
                    });
                }
            });
        });
    </script>
}